package com.lxyk.controller.student;

import com.github.pagehelper.PageInfo;
import com.lxyk.domain.Classes;
import com.lxyk.domain.StudentVO;
import com.lxyk.domain.StudentVO2;
import com.lxyk.domain.SysUser;
import com.lxyk.service.student.CommonService;
import com.lxyk.service.student.RefundService;

import com.lxyk.service.user.ClassesService;
import com.lxyk.service.user.UserService;
import com.lxyk.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

//退费

@Controller
@RequestMapping("/refund")
public class RefundController {

    @Autowired
    private UserService userService;

    @Autowired
    private RefundService refundService;

    @Autowired
    private ClassesService classesService;

    //根据id查询学生退费信息
    @RequestMapping("/findById")
    public String findRefundById(Model model, Integer id) {
        StudentVO2 student = refundService.findRefundById(id);
        model.addAttribute("student", student);
        return "student_refund";
    }

    //添加退费信息
    @RequestMapping("/addRefund")
    public String addRefund(StudentVO2 student) {
        Integer status = student.getStatusRefund();
        if (status == 0) {
            //添加退费信息
            refundService.addRefund(student);
            //修改学生退费标识
            refundService.updateStudent(student);
            //修改退费标识
            refundService.updateRefund(student);
            //修改班级人数
            classesService.outClass(student.getCid());
            return "forward:/common/findByCid";
        } else {
            return "forward:/common/findByCid";
        }
    }

    //查询所有退费信息
    @RequestMapping("/findAllRefund")
    public String findAllRefund(Model model) {
        //得到当前用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        //根据用户名查询用户信息
        SysUser user = userService.findByName(username);
        //根据用户id查询对应班级
        List<Classes> list = classesService.findByUser(user.getId());
        ArrayList<Integer> counts = new ArrayList<>();
        for (Classes classes : list) {
            Integer id = classes.getId();
            int count = refundService.findCountByCid(id);
            counts.add(count);
        }
        model.addAttribute("counts", counts);
        model.addAttribute("list", list);
        return "refund_base";
    }

    //查询当前班级所有退费信息
    @RequestMapping("/refundByCid")
    public String refundByCid(Model model, Integer cid, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageUtils.page(pageNum, pageSize);
        PageInfo<StudentVO2> pageInfo = refundService.refundByCid(cid, pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("cid", cid);
        return "refund_list";
    }

    //根据条件模糊查询退费信息
    @RequestMapping("/condition")
    public String findStudentByCondition(Model model, StudentVO2 vo, Integer cid, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageUtils.page(pageNum, pageSize);
        PageInfo<StudentVO2> pageInfo = refundService.findRefundByCondition(vo, pageNum, pageSize);
        pageInfo.setPageSize(pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("cid", cid);
        model.addAttribute("name", vo.getName());
        model.addAttribute("applyTime", vo.getApplyTime());
        model.addAttribute("consultTeacher", vo.getConsultTeacher());
        return "refund_list";
    }

}
